package org.edu.model;

import java.util.Date;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;


import javax.persistence.Transient;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.GenericGenerator;

/**
 * <h1>用户表</h1>
 * <p>该实例讲解了 <br>用户与用户信息 one-to-one <br>
 * 用户与主角色 many-to-one <br>
 * 用户与副角色 many-to-many <br>
 * 用户与部门many-to-one<br>
 * 并写明了配置要点
 * </p>
 * @author 谢圣强
 * @version 1.1
 * @see Role
 * @see Emp
 * @see UserDetailed
 */
@Entity
@Table(name = "T_USER")
public class User implements java.io.Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String account; // 帐号
	private String fullname;//别名
	private String pass;//"FDFMBFAMDCAMB@DCEAFFG@CD"; // 密码 md5的可逆加密 原值123 md5：202cb962ac59075b964b07152d234b70
	private Date lasttime;// 上次登录时间
	private Date offline;// 上次离线时间
	private String range;// 管理范围
	private String scheduling;// 排班
	
	private String ip;
	private String emailCapacity;
	private String fileCapacity;
	
	private String online;
	private String offtime;
	private UserDetailed userdetailedid;//详细信息
//
	private Emp empid;// many-one
//
	private Role roleid;// 主角色
//
	private Set<Role> sroleid;// 副角色

	@Id
	@GenericGenerator(name = "idGenerator", strategy = "assigned")
	@GeneratedValue(generator = "idGenerator")
	public String getAccount() {
		return account;
	}

	public void setAccount(String account) {
		this.account = account;
	}	

	@Column(name = "tb_lasttime")
	public Date getLasttime() {
		return lasttime;
	}

	public void setLasttime(Date lasttime) {
		this.lasttime = lasttime;
	}

	@Column(name = "tb_range")
	public String getRange() {
		return range;
	}

	public void setRange(String range) {
		this.range = range;
	}

	@Column(name = "tb_offline")
	public Date getOffline() {
		return offline;
	}

	@Column(name = "tb_schenduling")
	public String getScheduling() {
		return scheduling;
	}

	public void setScheduling(String scheduling) {
		this.scheduling = scheduling;
	}

	public void setOffline(Date offline) {
		this.offline = offline;
	}
	@Column(name = "tb_fullname")
	public String getFullname() {
		return fullname;
	}

	public void setFullname(String fullname) {
		this.fullname = fullname;
	}
	
	@Column(name = "tb_ip")
	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public String getEmailCapacity() {
		return emailCapacity;
	}

	public void setEmailCapacity(String emailCapacity) {
		this.emailCapacity = emailCapacity;
	}

	public String getFileCapacity() {
		return fileCapacity;
	}

	public void setFileCapacity(String fileCapacity) {
		this.fileCapacity = fileCapacity;
	}
	@Override
	public String toString() {
		// TODO Auto-generated method stub
//		StringBuffer sb= new StringBuffer();
//		sb.append(this.account).append("->").append(this.fullname).append("->").append(this.range).append("->").append(scheduling)
//		.append(emailCapacity).append("->").append(fileCapacity).append("->").append(ip);
//		return sb.toString();//<span class='glyphicon glyphicon-user'></span>
		return "{\'id\':\'" + this.account + "\',\'pId\':" + this.empid.getEmpno() + ",\'name\':\'" + this.account + "\',\'icon\':\'../dist/css/zTreeStyle/img/diy/10.png\'}";
	}
	
	
	
	@OneToOne(cascade=CascadeType.ALL)
	@PrimaryKeyJoinColumn
	public UserDetailed getUserdetailedid() {
		return userdetailedid;
	}

	public void setUserdetailedid(UserDetailed userdetailedid) {
		this.userdetailedid = userdetailedid;
	}
	
	@ManyToOne(targetEntity =org.edu.model.Emp.class,cascade={CascadeType.MERGE,CascadeType.REFRESH},optional=false)
	@JoinColumn(name="empid",updatable=false)
	public Emp getEmpid() {
		return empid;
	}

	public void setEmpid(Emp empid) {
		this.empid = empid;
	}

	@ManyToOne(targetEntity = org.edu.model.Role.class,cascade={CascadeType.MERGE},optional=false,fetch=FetchType.EAGER)
	@Fetch(FetchMode.JOIN)//急加载
	@JoinColumn(name = "roleid", updatable = false)
	public Role getRoleid() {
		return roleid;
	}

	public void setRoleid(Role roleid) {
		this.roleid = roleid;
	}

	@ManyToMany(targetEntity = org.edu.model.Role.class, cascade = {
			CascadeType.MERGE, CascadeType.PERSIST })
	@Fetch(FetchMode.JOIN)//急加载
	@JoinTable(name = "T_User_Role", joinColumns = { @JoinColumn(name = "user_id") }, inverseJoinColumns = { @JoinColumn(name = "role_id") })
	public Set<Role> getSroleid() {
		return sroleid;
	}

	public void setSroleid(Set<Role> sroleid) {
		this.sroleid = sroleid;
	}
	@Column(name="tb_Pass")
	@Fetch(FetchMode.JOIN)//急加载
	public String getPass() {
		return pass;
	}

	public void setPass(String pass) {
		this.pass = pass;
	}

	@Override
	public int hashCode() {
		// TODO Auto-generated method stub
		//System.out.println("user>pass>hashcode:"+this.pass+"user>hashcode"+this.account);
		int i=this.account.hashCode();
		return i;
	}

	@Override
	public boolean equals(Object obj) {
		// TODO Auto-generated method stub
		if(null==obj) return false;
		else if(obj instanceof User){
			User u=(User)obj;
			if(this.account.equals(u.getAccount())&&this.pass.equals(u.getPass()))//&&this.roleid.equals(u.getRoleid())&&this.empid.equals(u.getEmpid())
				return true;
		}
		return false;
	}
	@Transient
	public String getOnline() {
		return online;
	}

	public void setOnline(String online) {
		this.online = online;
	}
	@Transient
	public String getOfftime() {
		return offtime;
	}

	public void setOfftime(String offtime) {
		this.offtime = offtime;
	}
	
	

}